package cn.wsgfz;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

public class JVMObjectCase extends AbstractTestCase {

	public JVMObjectCase() {
		super("Serialization");
	}

	// 重写Write方法，只计数，不输出
	private class CountOutputStream extends OutputStream {
		long count = 0;

		@Override
		public void write(int b) throws IOException {
			count++;
		}
	}

	@Override
	public long countObjectSize(Object obj) {
		if (obj == null)
			return 0L;
		long result = 0L;
		try {
			CountOutputStream buffer = new CountOutputStream();
			ObjectOutputStream out = new ObjectOutputStream(buffer);
			out.writeObject(obj);
			out.close();
			result = buffer.count;
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException();
		}

		return result;
	}
}
